<%--
  @author Kusal Samarakoon
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="s" uri="/struts-tags" %>

<script type="text/javascript">

    $(function () {
        initData();
        $('select#gnDivisionId').bind('change', function (evt1) {
            initData();
        });
    });

    function initData() {
        var id = $('select#gnDivisionId').attr('value');
        var edit = $('#editMode').val();
        if ($('#editMode').val() == 3) {
            var mode = "associationView";
        }
        else if ($('#userGroup').val() == 'orip' && $('#editMode').val() == 2) {
            var mode = "defineAMAssociationApprove";
        }
        else {
            var mode = "toBeDefineAMLandList";
        }
        var type = "detail";
        $.getJSON('${pageContext.request.contextPath}/common/LandLookupService', {id: id, mode: mode, type: type}, function (data) {

            loadLandList(data.landList, edit);
        });
    }

    function loadLandList(landInfo, editmode) {

        var opt = '';
        opt += '<thead>' +
                '<tr class="table-header">' +
                '<th class="left-align">' + $('#referenceNO').val() + '</th>' +
                '<th class="left-align">' + $('#landlName').val() + '</th>' +
                '<th class="left-align">' + $('#action').val() + '</th>' +
                '</tr>' +
                '</thead>';
        opt += '<tbody>';
        if (landInfo.length > 0) {
            for (var i = 0; i < landInfo.length; i++) {
                opt += '<tr>' +
                        '<td class="left-align">' + landInfo[i].id + '</td>' +
                        '<td class="left-align">' + landInfo[i].nameOfTheLand + '</td>' +
                        '<td class="left-align"><a title="' + $('#associatetooltip').val() + '" href="initLandAssociationForDefineAM.do?editMode=' + editmode + '&landId=' + landInfo[i].id + '"><img src="${pageContext.request.contextPath}/images/interest-party.png" class="image-icon first-image-icon"/></a>' +
                        '</tr>';
            }
        }
        opt += '</tbody>';
        $('.land-table').html(opt);
        $('.land-table').dataTable({
            "sPaginationType": "full_numbers",
            "bDestroy": true,
            "oLanguage": {
                "sZeroRecords": $('#no-record').val(),
                "sLengthMenu": $('#display').val() + " _MENU_ " + $('#records').val(),
                "sSearch": $('#search').val(),
                "sInfo": $('#showing').val() + " _START_ " + $('#to').val() + " _END_ " + $('#of').val() + " _TOTAL_ " + $('#entries').val(),
                "sInfoEmpty": $('#no-record').val(),
                "oPaginate": {
                    "sFirst": $('#first').val(),
                    "sLast": $('#last').val(),
                    "sNext": $('#next').val(),
                    "sPrevious": $('#previous').val()
                }
            },
            "fnDrawCallback": function () {
                if ($('.dataTables_paginate span span.paginate_button').size()) {
                    $('.dataTables_paginate')[0].style.display = "block";
                } else {
                    $('.dataTables_paginate')[0].style.display = "none";
                }
            }
        });
    }


    $(function () {
        $('select#district').bind('change', function (evt1) {
            var id = $("select#district").attr("value");
            $.getJSON('${pageContext.request.contextPath}/common/DivisionLookupService', {id: id, mode: 2},
                    function (data) {
                        //var options1 = '<option value="0"> - Select  - </option> ';
                        var options1 = '<option value="0">' + $('#select').val() + '</option> ';
                        var ds = data.dsDivisionList;
                        for (var i = 0; i < ds.length; i++) {
                            options1 += '<option value="' + ds[i].optionValue + '">' + ds[i].optionDisplay + '</option>'

                        }
                        $("select#dsDivision").html(options1);
                        var options2 = '<option value="0">' + $('#select').val() + '</option> ';
                        $("select#gnDivisionId").html(options2);
                    });
        });
    });

    $(function () {
        $('select#dsDivision').bind('change', function (evt1) {
            var id = $("select#dsDivision").attr("value");
            $.getJSON('${pageContext.request.contextPath}/common/DivisionLookupService', {id: id, mode: 3},
                    function (data) {
                        //var options1 = '<option value="0"> - Select  - </option> ';
                        var options1 = '<option value="0">' + $('#select').val() + '</option> ';
                        var ds = data.gnDivisionList;
                        for (var i = 0; i < ds.length; i++) {
                            options1 += '<option value="' + ds[i].optionValue + '">' + ds[i].optionDisplay + '</option>'

                        }
                        $("select#gnDivisionId").html(options1);
                    });

        });
    });


</script>

<div id="land-registration-module-home-title" class="module-home-title">
    <s:a href="landRegistrationModule.do">
        <img src="${pageContext.request.contextPath}/images/land-reg.png" alt="Land Registration">
        <span>
            <s:if test="editMode==2">
                <s:label value="%{getText('approve_define_alienation_methods_for_lands.label')}"/>
            </s:if>
             <s:else>
                 <s:label value="%{getText('define_alienation_methods_for_lands.label')}"/>
             </s:else>
        </span>
    </s:a>
    <div id="back-button" align="right">
        <s:form><s:submit value="%{getText('back_btn.label')}" action="landRegistrationModule"/></s:form>
    </div>
</div>
<div id="land-association-list">
    <div class="page-title">
        <s:if test="editMode==2">
            <h2><s:label value="%{getText('approve_define_alienation_methods_for_lands.label')}"/></h2>
        </s:if>
        <s:else>
            <h2><s:label value="%{getText('define_alienation_methods_for_lands.label')}"/></h2>
        </s:else>

    </div>
    <s:if test="hasActionErrors() || hasActionMessages()">
        <div id="message-box" class="message-box">
            <div class="action-msg"><s:actionmessage/></div>
            <div class="action-err"><s:actionerror/></div>
        </div>
    </s:if>
    <table class="width-500-px margin-10-auto">

        <s:if test="#session.userGroup=='orip'">
            <tr>
                <td class="left-align"><s:label value="%{getText('district.label')}"/></td>
                <td><s:select list="districtList" name="districtName" headerKey="0" headerValue="%{getText('select.label')}"
                              id="district"/></td>

            </tr>
            <tr>
                <td class="left-align"><s:label value="%{getText('ds_division.label')}"/></td>
                <td><s:select name="dsDivisionName" id="dsDivision" headerKey="0" headerValue="%{getText('select.label')}"
                              list="dsDivisionList" cssClass="width-180-px"/>
                </td>
            </tr>
        </s:if>
        <s:else>
            <tr>
                <td><s:label value="%{getText('district.label')}"/></td>
                <td><s:textfield id="districtId" name="districtName" cssClass="width-200-px" disabled="true"/></td>
            </tr>
            <tr>
                <td><s:label value="%{getText('ds_division.label')}"/></td>
                <td><s:textfield id="dsDivisionId" name="dsDivisionName" cssClass="width-200-px" disabled="true"/></td>
            </tr>

        </s:else>
        <tr>
            <td><s:label value="%{getText('gn_division.label')}"/></td>
            <td><s:select id="gnDivisionId" name="gnDivisionId" list="gnDivisionList"
                          headerKey="0" headerValue="%{getText('select.label')}" cssClass="width-200-px"/></td>
        </tr>


    </table>
    <div id="land-list">
        <table class="width-100 margin-10-auto table land-table">

        </table>
    </div>
</div>

<s:hidden id="referenceNO" value="%{getText('slmsSerialNumber.label')}"/>
<s:hidden id="landlName" value="%{getText('name_of_land.label')}"/>
<s:hidden id="action" value="%{getText('action.label')}"/>
<s:hidden id="associatetooltip" value="%{getText('associate_tooltip')}"/>
<s:hidden id="viewtooltip" value="%{getText('view_tooltip')}"/>
<s:hidden id="viewLandDetailstooltip" value="%{getText('view_land_details_tooltip')}"/>

<%-- Localization text for data tables --%>
<s:hidden id="display" value="%{getText('display.label')}"/>
<s:hidden id="records" value="%{getText('records.label')}"/>
<s:hidden id="search" value="%{getText('search.label')}"/>
<s:hidden id="no-record" value="%{getText('no_record.label')}"/>
<s:hidden id="showing" value="%{getText('showing.label')}"/>
<s:hidden id="to" value="%{getText('to.label')}"/>
<s:hidden id="of" value="%{getText('of.label')}"/>
<s:hidden id="entries" value="%{getText('entries.label')}"/>
<s:hidden id="first" value="%{getText('first.label')}"/>
<s:hidden id="last" value="%{getText('last.label')}"/>
<s:hidden id="next" value="%{getText('next.label')}"/>
<s:hidden id="previous" value="%{getText('previous.label')}"/>

<s:hidden id="editMode" name="editMode"/>
<s:hidden id="userGroup" name="#session.userGroup"/>
<s:hidden id="select" value="%{getText('select.label')}"/>